//
//  AppDelegate.swift
//  1.0.Introduction
//
//  Created by joker on 15/7/20.
//  Copyright © 2015年 joker. All rights reserved.
//

import UIKit
enum CarClassification: String {
    case Estate = "Estate"
    case Hatchback = "Hatchback"
    case Saloon = "Saloon"
}

struct Car {
    let classification : CarClassification
}

struct cCar{
    let classification: CarClassification
    let year: Int
}

func classifyCar(car: cCar)
{
    switch car.classification {
    case .Estate where car.year >= 2013:
        println("This is a good and usable estate car")
    case .Hatchback where car.year >= 2010:
        println("This is an okay hatchback car")
    default:
        println("Unhandled case")
    }
}

class Person{
    var (firstName, lastName)  = ("", "")
    var age: Int
    var fullName: String
    
    init(firstName: String, lastName: String){
        self.firstName = firstName
        self.lastName = lastName
        self.fullName = ""
        self.age = 0
    }
    init(fullName: String, age: Int){
        self.fullName = fullName
        self.age = age
    }
}

postfix func ++ (inout person: Person) -> Person {
    let newPerson = Person(fullName: person.fullName, age: person.age)
    person.age++
    return newPerson
}

prefix func ++ (inout person: Person) -> Person {
    person.age++
    let newPerson = Person(fullName: person.fullName, age: person.age)
    return newPerson
}
func == (left: Person, right: Person) -> Bool {
    if left.firstName == right.firstName &&
        left.lastName == right.lastName{
            return true
    }
    return false
}
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func changeFirstNameOf(person: Person, to: String){
       person.firstName = to
    }
    
    
    typealias byte = UInt8
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        
        let vandad = Person(firstName: "Vandad", lastName: "Nahavandipoor")
        changeFirstNameOf(vandad, to: "VANDAD")
        /* vandad.firstName is still Vandad */
        
        let byte3 = 0b01010101 | 0b10101010 // = 0b11111111
        
        /* plus operator */
        let plus = 10 + 20 // =30
        
        /* minus operator */
        let minus = 20 - 10 // = 10
        
        /* multiplication operator */
        let multiplied = 10 * 20 // = 200
        
        /* division operator */
        let division = 10.0 / 3.0 // = 3.33333333
        
        
        println(byte3)
        println(plus)
        println(minus)
        println(multiplied)
        println(division)
        
        
        let andy = Person(firstName: "Andy", lastName: "Oram")
        let someoneElse = Person(firstName: "Andy", lastName: "Oram")
        
        if andy == someoneElse {
            println("They are the same")
        } else {
            println("They are not the same")
        }
        
        if andy === someoneElse {
            println("They are the same object")
        } else {
            println("They are not the same object")
        }
        
        var vandand = Person(fullName: "Vandad nahavandipoor", age: 29)
        var sameAgeVandand = vandand++
        println("vandad.age = \(vandand.age)")
        println("sameAgeVadad.age = \(sameAgeVandand.age)")
        
        let olderVandand = ++sameAgeVandand
        println("vandand.age = \(vandand.age)")
        println("sameAgeVandand.age = \(sameAgeVandand.age)")
        println("olderVandand.age = \(olderVandand.age)")
        
        
        //MARK: Declaring and Using Enumerations in Swift
        let volvoV50 = Car(classification: .Estate)
        
        switch volvoV50.classification {
        case .Estate:
            println("This is a good family car")
        case .Hatchback:
            println("Nice car, but not big enough for familuy")
        case .Saloon:
            println("I don’t understand this classification")
        }
        
        println(volvoV50.classification.rawValue)
        
        
        if let classification = CarClassification(rawValue: "Estate"){
            let volvoV50 = Car(classification: classification)
            println(volvoV50.classification.rawValue)
        }
        
        let oldEstate = cCar(classification: .Estate, year: 1980)
        let estate = cCar(classification: .Estate , year: 2010)
        let newEstate = cCar(classification: .Estate , year: 2015)
        let hatchback = cCar(classification: .Hatchback , year: 2013)
        let newSaloon = cCar(classification: .Saloon, year: 2015)
        
        classifyCar(oldEstate)
        classifyCar(estate)
        classifyCar(newEstate)
        classifyCar(hatchback)
        classifyCar(newSaloon)
        return true
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

